<%--
  Created by IntelliJ IDEA.
  User: 123
  Date: 2020/11/5
  Time: 20:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <table border="1px" id="ss">
        <tr>
            <td>ID</td>
            <td>姓名</td>
            <td>年龄</td>
            <td colspan="2" align="center">操作</td>
        </tr>
    </table>
    <button id="c" onclick="pre()">上一页</button>
    <span id="pageNum">1</span>当前页数
    <span id="pages">当前总页数</span>
    <button id="d" onclick="next()">下一页</button>
    <a href="add.jsp">添加联系人</a>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.1.js"></script>
<script type="text/javascript">
    var html = $("#pageNum").html();
    c(html);
    function c(num){
        $.ajax({
           url:"${pageContext.request.contextPath}/mains/mains_page",
            data:"pageSize=3&pageIndex="+num+"",
           type:"post",
           dataType:"json",
            success:function (s) {
               for (var i=0;i<s.list.length;i++){
                   $("#ss").append("<tr>  " +
                       "<td>"+s.list[i].id+"</td>\n" +
                       "<td>"+s.list[i].name+"</td>\n" +
                       "<td>"+s.list[i].age+"</td>" +
                       "<td><a href='javascript:void(0)'>删除</td>"+
                       "<td><a href=''>修改</td>"+
                       "</tr>")
               }
               $("#pages").html(s.pages)
               //  if (s.isFirstPage==true) {
               //     $("#ss").append("<tr><td>首页</td></tr>")
               //   }
               // if(s.isLastPage){
               //     $("#ss").append("<tr><td>末页</td></tr>")
               // }
               // if (s.hasPreviousPage==true){
               //    $("#ss").append("<tr><td><a href='show.jsp?pageIndex="+s.prePage+"&pageSize="+s.pageSize+"'>上一页<a></td></tr>")
               //  }
           }
        })
    }
    function next() {
        var html = $("#pageNum").html();
        var pages = $("#pages").html();
        if (html<pages) {
            num=parseInt(html);
            num++
            var html = $("#pageNum").html(num);
            $("#ss tr:gt(0)").remove()
            c(num)
        }
    }
    function pre() {
        var html = $("#pageNum").html();
        //console.log(html)
        if (html>=1) {
            num = parseInt(html);
            d=num-1;
            console.log(d)
            var html = $("#pageNum").html(d);
            $("#ss tr:gt(0)").remove()
            c(d)
        }
    }
</script>
</body>
</html>
